<?php
/**
 * @author Oliver Leisalu
 * @package WapBilling
 */
class WapBilling_Tele2 extends WapBillingOperator {
	
	protected $username;

	protected $password;

	private $receivedMsIsdn = false;
	
	protected $url;

	public function pay( $price, $description, $uid = 0, $logId ) {		
		
		if( $_SESSION['wapBilling']['tele2Web'] ) {
			$distributionType = '006';
		} else {
			$distributionType = '003';
		}
		
		$XtraData = '01;;;000000;'.$distributionType.';001;002;003;;00;;';
		
		$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<T2api:Call xmlns:T2api="urn:/T2api/Proto/Soap">
 <T2api:request>
   <T2api:url>CBG</T2api:url>
   <T2api:method>Purchase</T2api:method>
   <T2api:kwargs>
     <T2api:item><T2api:key>Version</T2api:key><T2api:valueUnsigned>208</T2api:valueUnsigned></T2api:item>
     <T2api:item><T2api:key>ContentType</T2api:key><T2api:valueUnsigned>1</T2api:valueUnsigned></T2api:item>
     <T2api:item><T2api:key>Currency</T2api:key><T2api:valueUnsigned>15</T2api:valueUnsigned></T2api:item>
     <T2api:item><T2api:key>Amount</T2api:key><T2api:valueUnsigned>'.round($price*100).'</T2api:valueUnsigned></T2api:item>
     <T2api:item><T2api:key>VAT</T2api:key><T2api:valueUnsigned>2000</T2api:valueUnsigned></T2api:item>
     <T2api:item><T2api:key>OriginatingCustomerId</T2api:key><T2api:valueString>'.'00'.$this->getMsisdn().'</T2api:valueString></T2api:item>
     <T2api:item><T2api:key>username</T2api:key><T2api:valueString>'.$this->username.'</T2api:valueString></T2api:item>
     <T2api:item><T2api:key>password</T2api:key><T2api:valueString>'.$this->password.'</T2api:valueString></T2api:item>
     <T2api:item><T2api:key>ContentDescription</T2api:key><T2api:valueString>'.$description.'</T2api:valueString></T2api:item>
     <T2api:item><T2api:key>ProviderTransactionId</T2api:key><T2api:valueUnsigned>'.$logId.'</T2api:valueUnsigned></T2api:item>
     <T2api:item><T2api:key>ReferenceID</T2api:key><T2api:valueUnsigned>0</T2api:valueUnsigned></T2api:item>
     <T2api:item><T2api:key>XtraData</T2api:key><T2api:valueString>'.$XtraData.'</T2api:valueString></T2api:item>
   </T2api:kwargs>
 </T2api:request>
</T2api:Call></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
		';
		
		$log = new DomDBLogger('billing_tele2'.( $_SESSION['wapBilling']['tele2Web'] ? '_web' : '' ));
		$logId = $log->log('pay-request', $xml );
		
		// -------------------------------
		// SENDING REQUEST
		$ch = curl_init( $this->url ); 
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		//curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);

		curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));

		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml );
		
		$result = curl_exec($ch);
		
		curl_close($ch);
		
		$logId = $log->log('pay-result-'.$logId, $result );
		
		if( $result === false ) {
			$this->lastError = 'Sending data failed!';
			return 3;	
		}
		// -------------------------------
		
		
		
		// -------------------------------
		// PARSING XML
		$status = -1;
		$transId = -1;
		
		$xml = new DomXml( $result );
		$response = $xml
			->getOneByNodeName('Envelope')
			->getOneByNodeName('Body')
			->getOneByNodeName('Response');
		$rc = (int)$response->getOneByNodeName('rc')->value;
		foreach( $response->getOneByNodeName('data')->getOneByNodeName('item')->getOneByNodeName('valueDict')->childNodes as $node ) {
			if( $node->getOneByNodeName('key')->value == 'Status' ) {
				$status = (int)$node->getOneByNodeName('valueUnsigned')->value;
			
			} else if( $node->getOneByNodeName('key')->value == 'TransactionId' ) {
				$transId = $node->getOneByNodeName('valueString')->value;
			}
		}
		// -------------------------------
		
		
		
		
		if( $rc === 200 && $status === 0 ) {
			return 0;
		}
		
		$this->lastError = 'Payment failed with error: '.$result['Status'].' ('.$result['TransactionId'].')';
		return 1;
		
	}

	public function getMsisdn() {
		if( $this->receivedMsIsdn !== false ) {
			return $this->receivedMsIsdn;
		}
		
		// if this is CBG client. if not, then phoneNR is sent through Cookie
		//if( (strpos($_SERVER['REMOTE_ADDR'],'213.102.') !== FALSE ) || (strpos($_SERVER['REMOTE_ADDR'],'212.247.') !== FALSE ) || (strpos($_SERVER['REMOTE_ADDR'], '90.137.132') !== FALSE) || (strpos($_SERVER['REMOTE_ADDR'],'90.137.133.') !== FALSE )) {
		
		if( $_SESSION['receivedMsisdnT2W'] ) {
			return $_SESSION['receivedMsisdnT2W'];
		}
		
		$host = gethostbyaddr( $_SERVER['REMOTE_ADDR'] );
		if( !$_COOKIE['x-up-calling-line-id'] && (strpos($host,'tele2') > 0 or strpos($host,'swip.net') > 0) )	{
			
			if( $_SESSION['receivedMsisdnT2'] ) {
				return $_SESSION['receivedMsisdnT2'];
			}
			
			//die('Vabandame! Teie telefoniga ei ole ajutiselt võimalik teenust kasutada!');
		
		if( $_COOKIE['ip-address'] ) {
			$originatingCustomerId = $_COOKIE['ip-address'];
		} else {
			$originatingCustomerId = $_SERVER['REMOTE_ADDR'];
		}
		
		$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAPENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" SOAPENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<T2api:Call xmlns:T2api="urn:/T2api/Proto/Soap">
<T2api:request>
<T2api:url>CBG</T2api:url>
<T2api:method>TranslateIP</T2api:method>
<T2api:kwargs>
<T2api:item><T2api:key>Version</T2api:key><T2api:valueUnsigned>208</T2api:valueUnsigned></T2api:item>
<T2api:item><T2api:key>OriginatingCustomerIP</T2api:key><T2api:valueString>'.$originatingCustomerId.'</T2api:valueString></T2api:item>
<T2api:item><T2api:key>username</T2api:key><T2api:valueString>'.$this->username.'</T2api:valueString></T2api:item>
<T2api:item><T2api:key>password</T2api:key><T2api:valueString>'.$this->password.'</T2api:valueString></T2api:item>
</T2api:kwargs>
</T2api:request>
</T2api:Call>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
			
		$log = new DomDBLogger('billing_tele2');
		$logId = $log->log('translateIp-request', $xml );
			
		// -------------------------------
		// SENDING REQUEST
		$ch = curl_init( $this->url ); 
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		//curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);

		curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));

		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml );
		
		$result = curl_exec($ch);
		
		$logId = $log->log('translateIp-result-'.$logId, $result );
		
		curl_close($ch);
		
		if( $result === false ) {
			$this->lastError = 'Sending data failed!';
			$this->receivedMsIsdn = '';
			return '';	
		}
		// -------------------------------
			
		
		
		
		
		
		
		// -------------------------------
		// PARSING XML
		$xml = new DomXml( $result );
		$msisdn = $xml
			->getOneByNodeName('Envelope')
			->getOneByNodeName('Body')
			->getOneByNodeName('Response')
			->getOneByNodeName('data')
			->getOneByNodeName('item')
			->getOneByNodeName('valueString')->value;
		$this->receivedMsIsdn = $msisdn;
		$_SESSION['receivedMsisdnT2'] = $this->receivedMsIsdn;
		// -------------------------------

		} else {
			$this->receivedMsIsdn = trim($_COOKIE['x-up-calling-line-id']);
		}
		return $this->receivedMsIsdn;
	}
}
?>